/**
 * 页面入口
 **/
var Kindeditor;
window.addEvent('load',function()
{
    //表单按钮事件
    $('ActionButton').addEvent('click', function(e)
    {
       if(CheckForm())
        {
            new Request_Binary(
            {
                //指定表单
                id:'Form1',
                //Ajax请求成功回调
                onSuccess:function($value){Form_Success($value);},
                //AJAX请求发生时回调
                onProcess:Form_Process
            }).send();
        }
        else
        {
            return false;
        }
    });

    //创建编辑器
    Kindeditor=KindEditor.create('#Content',
    {
        allowFileManager :true
    });
});

// ajax
var xmlHttp = false;
try {
  xmlHttp = new XMLHttpRequest();
} catch (trymicrosoft) {
  try {
    xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (othermicrosoft) {
    try {
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (failed) {
      xmlHttp = false;
    }
  }
}


function callServer() {
  var title=document.getElementById("Title").value;
  	if(title==""){
		$("title_aj").innerHTML="<span style='border:1px solid #e6e2e2;  background:#fff'> 标题不能为空 </span>";
		return false;
	}
	else{
		$("title_aj").innerHTML="";
	}

  var send_string="Title="+title+"";
  send_string= encodeURI(send_string);
  var url = "server.php";
  xmlHttp.open("post", url, true);
  xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  xmlHttp.onreadystatechange = updatePage;
  xmlHttp.send(send_string);
}

function updatePage() {
  if (xmlHttp.readyState == 4) {
	  if(xmlHttp.status==200){
		var response = xmlHttp.responseText;
	   $("title_aj").innerHTML=response;
	  }else{
	  }
  }else{
	 // alert(xmlHttp.readyState);
  }
}

/**
 * 函数片断
 **/
//表单数据合法性验证
function CheckForm()
{
	var Title=$('Title');
    if(Title.value=='')
	{
		malert('请填写标题');
		Title.focus();
		return false;
	}
	

	var classID=$('FirstClass');
    if(classID.value==''||classID.value=='0')
	{
		malert('请选择问题一级类别');
		classID.focus();
		return false;
	}
	
    return true;
}

//表单AJAX提交后回调函数-成功
function Form_Success($value)
{
    var result=JSON.decode($value);
    switch(result.Number)
    {
        case '0':       
            MUI.App.Process.Update
            ({
                content:'信息发布成功',
                isHaveCloseButton:false,
                onSuccess:function()
                {
                    MUI.App.Process.Close
                    ({
                        onSuccess:function()
                        {
                            window.location='add.php';
                        }
                    });
                }
            });       
        break;

        default:
            MUI.App.Process.Update
            ({
                content:result.Description,
                isHaveCloseButton:true             
            });
    }
}

//表单数据提交时回调函数
function Form_Process()
{
    MUI.App.Process.Call();
}